<template>
  <div class="warpper">
    <div class="toolbar" style="float: right;">
      <el-button type="primary" v-permission="['shipmenterms.store']" plain @click="add" style="margin:0 30px;">+New</el-button>
    </div>
    <div class="table">
      <el-table :data="tableData" size="small" stripe border style="width: 100%">
        <!-- <el-table-column prop="id" label="Id" width="50" align="center" /> -->

        <el-table-column prop="name" label="Shipment Term Name" align="center" />

        <el-table-column prop="status" label="Status" align="center" />

        <el-table-column label="Shipment term" align="center">
          <template v-slot="scope">
            {{scope.row.shipmentterm}}{{scope.row.shipmenttermday}}
          </template>
        </el-table-column>

        <el-table-column prop="description" label="Description" align="center" />

        <el-table-column prop="adminame" label="Creator" align="center" />

        <el-table-column label="Operate" align="center">
          <template slot-scope="scope">
            <el-button plain v-permission="['shipmenterms.update']" @click="edit(scope.row.id)">Edit</el-button>
            <el-button plain v-permission="['shipmenterms.destroy']" type="danger" @click="del(scope.row.id)">Delete</el-button>
          </template>
        </el-table-column>
      </el-table>

    </div>

    <div class="page">
      <el-pagination background :current-page="page.current_page" :page-sizes="page.sizes" :page-size="page.per_page"
        layout="prev, pager, next" :total="page.total" @size-change="sizeChange" @current-change="currentChange" />
    </div>


    <el-dialog :title="title" :visible.sync="dialogFormVisible" :close-on-click-modal="false" width="70%">
      <el-form v-if="dialogFormVisible" ref="ruleForm" :model="formData" :rules="rules" label-width="140px">

        <el-form-item label="name" prop="name">
          <el-input v-model="formData.name" :disabled="isEdit" style="width: 400px" />
        </el-form-item>

        <el-form-item label="Shipment term" prop="shipmentterm">
          <el-input-number v-model="formData.shipmentterm" :min="1" :max="10000" style="width: 200px"></el-input-number>
          <el-select v-model="formData.shipmenttermday" style="width: 200px">
            <el-option v-for="item in shipmenttermday" :key="item.id" :label="item.name" :value="item.name" />
          </el-select>
        </el-form-item>

        <el-form-item label="description" prop="description">
          <el-input v-model="formData.description" style="width: 400px" type="textarea" :rows="2" />
        </el-form-item>

      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">cancel</el-button>
        <el-button type="primary" @click="saveData()">Save</el-button>
      </div>
    </el-dialog>

  </div>
</template>

<script>
  import CURD from "@/mixin/CURD";
  import {
    update
  } from "@/api/admin";
  import {
    index as getRoles
  } from "@/api/role";
  import {
    store as uploadImg
  } from "@/api/media";
  import UploadXls from "@/components/UploadXls";
  export default {
    name: "AdminIndex",
    components: {
      UploadXls: UploadXls
    },
    mixins: [CURD],
    data() {
      return {
        module: "shipmenterm",
        newTitle: "New Shipment term",
        editTitle: "Edit Shipment term",
        imageUrl: "",
        dialogFormPasswordVisible: false,
        password: "",
        id: "",
        roles: [],
        formData: {
          shipmentterm: 1,
          shipmenttermday: "day"
        },
        shipmenttermday: [{
          id: 1,
          name: "day",
        }, {
          id: 2,
          name: "week",
        }, {
          id: 3,
          name: "month",
        }, {
          id: 4,
          name: "year",
        }],
        isUploadFile: false,
        importDialogVisible: false,
        fileReader: new FileReader(),
        isShow: false,
        squareUrl: "https://cube.elemecdn.com/9/c2/f0ee8a3c7c9638a54940382568c9dpng.png"
      };
    },
    async created() {

    },

    methods: {
      uploadData() {
        this.isShow = true;
      },
      async edit(id) {
        const {
          show
        } = require(`@/api/${this.module}`);
        const {
          data
        } = await show(id);
        let result = data;
        this.formData = result;
        this.isNew = false;
        this.isEdit = true;
        this.setTitle();
        this.dialogFormVisible = true;
      },
      async changeStatus(content) {
        this.$confirm("This operation will modify the user status. Do you want to continue?", "prompt", {
            confirmButtonText: "confirm",
            cancelButtonText: "cancel",
            type: "warning"
          })
          .then(async () => {
            const data = {
              id: content.id,
              status: content.status ? 1 : 0,
              action: "status"
            };
            const {
              info
            } = await update(data);
            this.$message.success(info);
          })
          .catch(() => {
            // 返回原来的状态
            content.status = !content.status;
          });
      },
      async avatarUpload(options) {
        const file = options.file;
        if (file) {
          this.fileReader.readAsDataURL(file);
        }
        this.fileReader.onload = async () => {
          const formData = new FormData();
          formData.append("file", file);
          uploadImg(formData)
            .then((res) => {
              const {
                data
              } = res;
              this.formData.avatar = data.url;
              this.save("ruleForm");
            })
            .catch((err) => {
              options.onError(err);
            });
        };
      },
      updatePassword(id) {
        this.id = id;
        this.dialogFormPasswordVisible = true;
      },
      async savePassword() {
        if (this.password.length >= 6 && this.password.length <= 20) {
          const data = {
            id: this.id,
            action: "reset",
            password: this.password
          };
          const {
            info
          } = await update(data);
          this.$message.success(info);
          this.dialogFormPasswordVisible = false;
        } else {
          this.$message.error("The password length must be between 6-20 characters");
        }
      },
      changeAvatar(file, fileList) {
        const reader = new window.FileReader();
        reader.readAsDataURL(new Blob([file.raw]));
        reader.onload = (e) => {
          this.formData.avatar = reader.result;
          this.imageUrl = reader.result;
          this.isUploadFile = true;
        };
      },
      saveData() {
        if (this.isUploadFile) {
          this.$refs.uploadAvatar.submit();
        } else {
          this.save("ruleForm");
        }
      }
    }
  };
</script>

<style>
  .table,
  .toolbar,
  .page {
    margin: 20px;
  }

  .toolbar {
    margin-top: 10px;
    ;
    padding: 5px;
  }

  .table {
    margin-bottom: 10px;
  }

  .page {
    margin-top: 10px;
  }

  .page {
    text-align: center;
  }

  .el-form table tbody {
    width: 100%;
  }

  .el-form .header {
    box-sizing: border-box;
    border: 1px solid #ccc;
    background-color: #09f;
    color: #fff;
    height: 44px;
    display: flex;
    flex-direction: row;
  }

  .el-form .header .title {
    margin: auto;
    font-weight: bold;
  }

  .el-form .content {
    display: flex;
    box-sizing: border-box;
    border: 1px solid #ccc;
    background-color: #0cf;
    color: #fff;
    height: 44px;
    flex-direction: row;
  }

  .el-form .content div {
    margin: auto;
    border-radius: 0px;
  }

  .avatar-uploader .el-upload {
    border: 1px dashed #d9d9d9;
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
  }

  .avatar-uploader .el-upload:hover {
    border-color: #409eff;
  }

  .avatar-uploader-icon {
    font-size: 28px;
    color: #8c939d;
    width: 178px;
    height: 178px;
    line-height: 178px;
    text-align: center;
  }

  .avatar {
    width: 178px;
    height: 178px;
    display: block;
  }
</style>